﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;

namespace RegionSplittor.Utils
{
	public class SmokeDetectorHeight
	{
		public static double GetSetHeight(ElementId id, Autodesk.Revit.DB.Document doc)
		{
			Element element = doc.GetElement(id);
			IEnumerator<GeometryObject> enumerator = element.get_Geometry(new Options()).GetEnumerator();
			double num = double.MaxValue;
			bool flag = false;
			while (enumerator.MoveNext())
			{
				GeometryObject geometryObject = enumerator.Current;
				if (geometryObject is Solid)
				{
					Solid solid = geometryObject as Solid;
					ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_Floors);
					ElementIntersectsSolidFilter elementIntersectsSolidFilter = new ElementIntersectsSolidFilter(solid);
					LogicalAndFilter logicalAndFilter = new LogicalAndFilter(elementCategoryFilter, elementIntersectsSolidFilter);
					foreach (Element element2 in new FilteredElementCollector(doc).WherePasses(logicalAndFilter).ToElements().ToList<Element>())
					{
						if (element2 is Floor)
						{
							double eleParamDoubleValue = BeamOperator.GetEleParamDoubleValue(element2 as Floor, "底部高程");
							if (eleParamDoubleValue < num)
							{
								num = eleParamDoubleValue;
								flag = true;
							}
						}
					}
				}
			}
			if (!flag)
			{
				num = BeamOperator.GetEleParamDoubleValue(element, "顶部高程");
			}
			return num;
		}

		public static double GetSetHeight(Autodesk.Revit.DB.Document MainDoc, CurveLoop curveLoop, Autodesk.Revit.DB.Document doc, double valueLevel = 0.0)
		{
			if (curveLoop == null)
			{
				return 0.0;
			}
			double num = double.MaxValue;
			bool flag = false;
			List<CurveLoop> list = new List<CurveLoop>();
			list.Add(curveLoop);
			Solid solid = null;
			try
			{
				solid = GeometryCreationUtilities.CreateExtrusionGeometry(list, XYZ.BasisZ, SmokeDetectorHeight.floorHeight);
			}
			catch (Exception)
			{
			}
			Document document;
			LinkDocumentOperator.GetSpecifyLnkDocument(MainDoc, "S", out document);
			Document document2;
			LinkDocumentOperator.GetSpecifyLnkDocument(MainDoc, "A", out document2);
			ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_Floors);
			ElementIntersectsSolidFilter elementIntersectsSolidFilter = new ElementIntersectsSolidFilter(solid);
			LogicalAndFilter logicalAndFilter = new LogicalAndFilter(elementCategoryFilter, elementIntersectsSolidFilter);
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document2);
			filteredElementCollector.WhereElementIsNotElementType();
			List<Element> list2 = filteredElementCollector.WherePasses(logicalAndFilter).ToElements().ToList<Element>();
			if (list2.Count == 0)
			{
				FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(document2);
				filteredElementCollector2.WhereElementIsNotElementType();
				list2 = filteredElementCollector2.WherePasses(logicalAndFilter).ToElements().ToList<Element>();
				if (list2.Count == 0)
				{
					FilteredElementCollector filteredElementCollector3 = new FilteredElementCollector(MainDoc);
					filteredElementCollector3.WhereElementIsNotElementType();
					list2 = filteredElementCollector3.WherePasses(logicalAndFilter).ToElements().ToList<Element>();
				}
			}
			solid.Dispose();
			filteredElementCollector.Dispose();
			foreach (Element element in list2)
			{
				if (element is Floor)
				{
					double eleParamDoubleValue = BeamOperator.GetEleParamDoubleValue(element as Floor, "底部高程");
					if (eleParamDoubleValue < num)
					{
						num = eleParamDoubleValue;
						flag = true;
					}
				}
			}
			if (!flag)
			{
				num = valueLevel + 8.2020997375328086;
			}
			FilterMemeryManager.ReleaseFilers(new List<ElementFilter>
			{
				elementCategoryFilter,
				elementIntersectsSolidFilter,
				logicalAndFilter
			});
			return num;
		}

		private static double floorHeight = 22.965879265091864;
	}
}
